package com.xujianlong.day03;

import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Flink09_TransForm_Process {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> dataStreamSource = env.socketTextStream("hadoop102", 9999);

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOneStream = dataStreamSource.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void processElement(String s, Context context, Collector<Tuple2<String, Integer>> collector) throws Exception {
                String[] strs = s.split(" ");
                for (String str : strs) {
                    collector.collect(Tuple2.of(str, 1));
                }

            }
        });

        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = wordToOneStream.keyBy(0);
        keyedStream.process(new KeyedProcessFunction<Tuple, Tuple2<String, Integer>, Tuple2<String,Integer>>() {
            private HashMap<String,Integer> map=new HashMap<>();
            @Override
            public void processElement(Tuple2<String, Integer> value, Context context, Collector<Tuple2<String, Integer>> out) throws Exception {
                if(!map.containsKey(value.f0)){
                    map.put(value.f0,value.f1);
                }else{
                    Integer lastSum = map.get(value.f0);
                    lastSum+=value.f1
                            ;
                    map.put(value.f0,lastSum);
                }
                out.collect(Tuple2.of(value.f0,map.get(value.f1)));
            }
        }).print();

        env.execute();
    }
}
